package tech.aistar.corejava.day15.homework;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
1. 写两线程，
一个线程负责打印数字到 52
另一个线程负责打印字母A到Z
最终打印出来的结果是：
12A34B56C...5152Z
 *
 * @CreateDate: 2019/8/5
 * @Version: 1.0
 */
public class Demo01 {
    public static void main(String[] args) {
        Painter p = new Painter();
        new Thread() {
            @Override
            public void run() {
                p.print01();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                p.print02();
            }
        }.start();
    }
}

class Painter {

    public void print01() {
        synchronized (this) {  //同步代码块,锁对象为调用这个方法的对象
            for (int i = 1; i <= 52; i++) {
                System.out.print(i);
                if (i % 2 == 0) {
                    notifyAll();
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    public void print02() {
        synchronized (this) {
            for (char ch = 'A'; ch <= 'Z'; ch++) {
                System.out.print(ch);
                notifyAll(); //唤醒所有线程
                try {
                    wait();   //让该线程等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
